package com.simplenix.nicasio.tags;

import com.simplenix.nicasio.def.Feature;
import com.simplenix.nicasio.def.Module;
import com.simplenix.nicasio.misc.AppException;
import com.simplenix.nicasio.misc.FileUtil;
import com.simplenix.nicasio.sys.SystemDef;
import com.simplenix.nicasio.textparser.TextParser;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 *
 * @author fronald
 */
public class Footer extends SimpleTagSupport {

	private java.lang.String featureName;

	private java.lang.String moduleName;

	/**
	 * Called by the container to invoke this tag.
	 * The implementation of this method is provided by the tag library developer,
	 * and handles all tag processing, body iteration, etc.
	 */
	@Override
	public void doTag() throws JspException {
		JspWriter out = getJspContext().getOut();

		try {
			JspFragment f = getJspBody();
			if (f != null) {
				f.invoke(out);
			}

			List<java.lang.String> htmlLines = FileUtil.readTextLines(SystemDef.getInstance().getAppPath() + "/nicasio/layout/footer.html", true);
			java.lang.String html = "";
			for (java.lang.String line : htmlLines) {
				html += line + "\n";
			}

			html = TextParser.parseString(html);
			Module module = SystemDef.getInstance().getModules().getModuleByName(this.getModuleName());
			Feature feature = SystemDef.getInstance().getModules().getFeaturesByModule(module).getFeatureByName(this.getFeatureName());

			html = TextParser.parseKeyTags(html, feature, (HttpServletRequest) ((PageContext) this.getJspContext()).getRequest());
			out.println(html);

		} catch (AppException ex) {
			throw new JspException("Error in Header tag", ex);
		} catch (java.io.IOException ex) {
			throw new JspException("Error in Header tag", ex);
		}
	}

	/**
	 * @return the featureName
	 */
	public java.lang.String getFeatureName() {
		return featureName;
	}

	/**
	 * @param featureName the featureName to set
	 */
	public void setFeatureName(java.lang.String featureName) {
		this.featureName = featureName;
	}

	/**
	 * @return the moduleName
	 */
	public java.lang.String getModuleName() {
		return moduleName;
	}

	/**
	 * @param moduleName the moduleName to set
	 */
	public void setModuleName(java.lang.String moduleName) {
		this.moduleName = moduleName;
	}

}
